<?php
class koudaiControl extends skymvc{
	
	public function __construct(){
		
		parent::__construct();
	}
	
	public function onDefault(){
		$shop=M("koudai")->selectRow("siteid=".SITEID);
		if(empty($shop)) $this->goAll("口袋店还没开通",1);
		$open=shopopen($shop['yystart'],$shop['yyend']);
		if($shop['status']!=2){
			$this->goAll("口袋店还未开始营业",1);
		}
		//M("koudai_product")->update(array("status"=>2)," shopid=".$shop['id']." AND status=4 AND total_num > 0 ");
		$where=" shopid=".$shop['id']." AND status=2 ";
		$catid=get("catid",'i');
		$baseurl="/index.php?m=koudai";
		if($catid){
			$where.=" AND catid=".$catid;
			$baseurl.="&catid=".$catid;
		}
		$keyword=get_post('keyword','h');
		if($keyword){
			$ids=M("product")->selectCols(array(
				"where"=>"title like '%".$keyword."%'",
				"fields"=>"id",
				"limit"=>100
			));
			 
			if($ids){
				$where.=" AND productid in("._implode($ids).")";
			}else{
				$where.=" 1=2 ";
			}
			$baseurl.="&keyword=".urlencode($keyword);
		}
		$index='';
		switch(get('index')){
			case 'asc':
					$index='asc';
				break;
			case 'desc':
					$index='desc';
				break;
		}
		switch(get('orderby')){
			case "price":
				$order=" id DESC";
				break;
			case "buy_num":
				$order=" buy_num $index";
				break;
			default:
				$order=" orderindex DESC,id DESC";
				break;
		}
		
		$url=$baseurl;
		$limit=24;
		$start=get('per_page','i');
		$option=array(
			"where"=>$where." AND total_num>0 ",
			"order"=>$order,
			"start"=>$start,
			"limit"=>$limit
			 
		);
	   	$rscount=true;
		$product=M("koudai_product")->getlist($option,$rscount);
	  	if($product && get('orderby')=='price'){
			foreach($product as $k=>$v){
				$prices[$k]=$v['price'];				
			}
			array_multisort($product,$index=='asc'?SORT_ASC:SORT_DESC,$prices);
		}
		/*
		$option=array(
			"where"=>$where." AND total_num=0 ",
			"order"=>$order
		);
		$nproduct=M("koudai_product")->getlist($option);
		if($product && $nproduct){
			$product=array_merge($product,$nproduct);
		}elseif(!empty($nproduct)){
			$product=$nproduct;
		}
		*/
		$cart=M("koudai_order_cart")->getListByUser();
		if($product){
				foreach($product as $k=>$v){
					if(isset($cart['product'][$v['id']])){
						$v['incart']=1;
						$v['cart']=$cart['product'][$v['id']];
					}else{
						$v['incart']=0;
					}
					$product[$k]=$v;
				}
			}
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$per_page=$start+$limit;
		$per_page=$per_page>=$rscount?0:$per_page; 
		$catlist=M("koudai_category")->children(array("where"=>" status=2 AND shopid=".$shop['id']));
		$this->smarty->goassign(array(
			"shop"=>$shop,
			"prolist"=>$product,
			"carturl"=>"/index.php?m=koudai_order_cart&shopid=".$shop['id'],
			"baseurl"=>$baseurl,
			"pagelist"=>$pagelist,
			"catlist"=>$catlist,
			"per_page"=>$per_page,
			"cart"=>$cart
		));
		$this->smarty->display("koudai/index.html");
	}
		
}

?>